﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace AlertSystem.Web
{
    public class Global : System.Web.HttpApplication
    {

        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            /*
            try
            {
                int viewpage = 0;
                if (!System.IO.File.Exists(Server.MapPath("~/App_Data/Counthits.txt")))
                {
                    viewpage = 1;
                }
                else
                {
                    // Đọc dử liều từ file count_visit.txt
                    System.IO.StreamReader read = new System.IO.StreamReader(Server.MapPath("~/App_Data/Counthits.txt"));
                    viewpage = int.Parse(read.ReadLine());
                    read.Close();
                }
                // khóa website
                Application.Lock();

                // gán biến Application viewpage
                Application["viewpage"] = viewpage;

                // Mở khóa website
                Application.UnLock();

                // Lưu dử liệu vào file  count_visit.txt
                System.IO.StreamWriter writer = new System.IO.StreamWriter(Server.MapPath("~/App_Data/Counthits.txt"));
                writer.WriteLine(viewpage);
                writer.Close();
            }
            catch(Exception ex)
            {
                Business.SysLog.InsertLog(ex);
            }
             */
        }

        void Application_End(object sender, EventArgs e)
        {
            try
            {
                //  Code that runs on application shutdown
                //Lưu dử liệu vào file  count_visit.txt
                using (System.IO.StreamWriter writer = new System.IO.StreamWriter(Server.MapPath("~/App_Data/Counthits.txt")))
                {
                    writer.WriteLine(Application["viewpage"].ToString());
                    writer.Close();
                }
                Application["visit"] = null;
            }
            catch { }
        }

        void Application_Error(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs

        }

        void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started
            try
            {
                if (Application["visit"] == null) Application["visit"] = 1;
                else
                {
                    Application["visit"] = int.Parse(Application["visit"].ToString()) + 1;
                }
                //System.IO.StreamReader read = new System.IO.StreamReader(Server.MapPath("~/App_Data/Counthits.txt"));
                //int viewpage = int.Parse(read.ReadLine()) + 1;
                //read.Close();
                //// khóa website
                //Application.Lock();

                // gán biến Application viewpage
                if (Application["viewpage"] == null) 
                    Application["viewpage"] = 1;
                else
                    Application["viewpage"] = int.Parse(Application["viewpage"].ToString()) + 1;

                // Mở khóa website
                //Application.UnLock();

                // Lưu dử liệu vào file  count_visit.txt
                //System.IO.StreamWriter writer = new System.IO.StreamWriter(Server.MapPath("~/App_Data/Counthits.txt"));
                //writer.WriteLine(viewpage);
                //writer.Close();
            }
            catch
            {

            }
        }

        void Session_End(object sender, EventArgs e)
        {
            // Code that runs when a session ends. 
            // Note: The Session_End event is raised only when the sessionstate mode
            // is set to InProc in the Web.config file. If session mode is set to StateServer 
            // or SQLServer, the event is not raised.
            Application["visit"] = int.Parse(Application["visit"].ToString()) - 1;
        }

    }
}
